﻿using AutoMapper;
using ERPSystem.Common.Domains.supplier;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;

namespace BasicInformationWrite.API.BasicInformationCommons.suppliers
{
    public class supplierCreateHander : IRequestHandler<supplierCreateCommand, Result>
    {
        private readonly IBaseRepository<Supplier> _supplierRe;
        private readonly IMapper mapper;
        public supplierCreateHander(IBaseRepository<Supplier> supplierRe, IMapper mapper)
        {
            _supplierRe = supplierRe;
            this.mapper = mapper;
        }


        public async Task<Result> Handle(supplierCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added};

            try
            {
                var supplier = mapper.Map<Supplier>(request);
                supplier.CreatedOwnerId = "张三";
                supplier.CreatedTime = DateTime.Now;
                var list = await _supplierRe.Add(supplier);
                if(list>0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }

            }
            catch (Exception)
            {

                throw;
            }
            return result;


        }




    }
}
